package org.eduapp.trains.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

public class UtilsReflect {
	
	public static HashMap<String, Object> getAllComponentsName(String preKey, Object f) {
		HashMap<String, Object> data = new HashMap<String, Object>();
		if (f != null) {
			Field[] fields = f.getClass().getDeclaredFields();
			for(int i = 0 , len = fields.length; i < len; i++) {
				String varName = fields[i].getName();
				try {
					boolean accessFlag = fields[i].isAccessible();
					fields[i].setAccessible(true);
					Method method = f.getClass().getMethod(
							"get" + varName.substring(0,1).toUpperCase() + varName.substring(1), new Class[]{});
					if (method != null) {
						data.put(preKey + varName, method.invoke(f, new Object[]{}));
						fields[i].setAccessible(accessFlag);
					}
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				} catch (IllegalAccessException ex) {
					ex.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return data;
	}
	
	public static HashMap<String, Object> getAllComponentsName(Object f) {
		return getAllComponentsName("", f);
	}
	
	public static void main(String[] args) {
		
	}

}
